package com.brtbeacon.wx.demo.mapview;

import android.Manifest;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import okhttp3.HttpUrl;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private final static int CODE_REQUEST_PERMISSIONS = 999;
    private final static String KEY_URL = "key_url";
    private EditText editURL;
    private SharedPreferences sp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.btn_start).setOnClickListener(this);
        findViewById(R.id.btn_lock_start).setOnClickListener(this);
        editURL = findViewById(R.id.edit_url);

        sp = getPreferences(0);
        String url = sp.getString(KEY_URL, "");
        if (!TextUtils.isEmpty(url)) {
            editURL.setText(url);
        }

        checkPermissions();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_start: {
                String url = editURL.getText().toString();
                if (TextUtils.isEmpty(url)) {
                    Toast.makeText(this, "请填写地图链接！", Toast.LENGTH_SHORT).show();
                    break;
                }
                sp.edit().putString(KEY_URL, url).commit();
                url = getAppendURL(url);
                if (TextUtils.isEmpty(url))
                    return;
                MyDefaultWxMapActivity.startActivity(this, url);
                break;
            }
        }
    }

    /*
    private void checkPermissions() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
                requestPermissions(new String[]{Manifest.permission.READ_PHONE_STATE}, CODE_REQUEST_PERMISSIONS);
            }
        }
    }
     */
    private void checkPermissions() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

            String[] permissions = new String[] {
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.CAMERA
            };

            List<String> checkPermissionList = new ArrayList<>();

            for (String permission: permissions) {
                if (this.checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
                    checkPermissionList.add(permission);
                }
            }

            if (!checkPermissionList.isEmpty()) {
                requestPermissions(checkPermissionList.toArray(new String[1]), CODE_REQUEST_PERMISSIONS);
            }
        }
    }

    //  给链接中添加当前设备IMEI标识；
    private String getAppendURL(String url) {
        TelephonyManager telManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
            Toast.makeText(this, "缺少获取设备唯一标识的权限!", Toast.LENGTH_SHORT).show();
            return "";
        }

        String imei = telManager.getDeviceId();
        if (TextUtils.isEmpty(imei)) {
            Toast.makeText(this, "无法获取设备唯一标识!", Toast.LENGTH_SHORT).show();
            return "";
        }

        try {
            HttpUrl httpUrl = HttpUrl.parse(url);
            httpUrl = httpUrl.newBuilder().addQueryParameter("mac", imei).build();
            return httpUrl.toString();
        } catch (Exception e){
            Toast.makeText(this, "H5地址无效，请重新输入!", Toast.LENGTH_SHORT).show();
        }

        return "";
    }



}
